
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.*;
import java.util.concurrent.*;

public class Machine extends Thread {

	public Receiver r;
	public Sender s;
	public ArrayBlockingQueue<MachineData> queue;
	
	public Machine(ArrayList<MachineData> mData, int m) {
		
		DatagramSocket clientSocket = null;
		queue = new ArrayBlockingQueue<MachineData>(Global.size);
		
		try {
			clientSocket = new DatagramSocket(mData.get(m).port);
		}
		catch(SocketException e) {
			e.printStackTrace();
		}
		
		r = new Receiver(mData, m, clientSocket, queue);
		s = new Sender(mData, m, clientSocket, queue);
	}
	
	public void run() {

		r.start();

		//wait for all receivers to be up and running before sending messages
		if(Global.demo) {
			if(Global.starter) {
				try {
					Thread.sleep(Global.grace * 1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			else {
				while(queue.size() == 0) {	
				}
			}
		}

		s.start();
	}
}
